package com.txr.news.service.impl;

import com.txr.news.customexception.BusinessException;
import com.txr.news.customexception.SystemException;
import com.txr.news.mapper.ArticleCateMapper;
import com.txr.news.pojo.ExType;
import com.txr.news.pojo.Result;
import com.txr.news.pojo.entity.ArticleCate;
import com.txr.news.pojo.entity.ArticleCateExample;
import com.txr.news.service.ArticleCateService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class ArticleCateServiceImpl implements ArticleCateService {
    ArticleCateMapper articleCateMapper;

    /**
     * 获取所有类别信息：
     * 1、数据库查询全部类别信息
     * 2、判断是否有类别信息
     * @return
     */
    @Override
    public List<ArticleCate> getCates() {
        ArticleCateExample articleCateExample = new ArticleCateExample();
        articleCateExample.createCriteria().andIsDeleteEqualTo(false);
        List<ArticleCate> cateList = articleCateMapper.selectByExample(articleCateExample);
        if (cateList == null || cateList.isEmpty()){
            throw new BusinessException("cate not found", ExType.BussRecordNotFound);
        }
        return cateList;
    }

    /**
     * 新增类别：
     * 1、根据类别名查询数据库是否有同名类别，如果有返回 false
     * 2、没有就添加到数据库并返回 true
     * @param articleCate
     * @return
     */
    @Override
    public Boolean addCate(ArticleCate articleCate) {
        ArticleCateExample articleCateExample = new ArticleCateExample();

        articleCateExample.createCriteria().andNameEqualTo(articleCate.getName());
        List<ArticleCate> cateList = articleCateMapper.selectByExample(articleCateExample);

        if (cateList != null && !cateList.isEmpty()){
            return false;
        }

        int flag = articleCateMapper.insertSelective(articleCate);
        if (flag != 1){
            throw new SystemException("insert failed", ExType.SystemInsertFail);
        }
        return true;
    }

    /**
     * 删除分类：
     * 1、根据 id 查询数据库谁否有该类别 如果没有 返回false
     * 2、如果有 进行删除操作：把is_delete修改为1
     * @param id
     * @return
     */
    @Override
    public Boolean deleteCate(String id) {
        Integer newid = Integer.parseInt(id);
        ArticleCate articleCate = articleCateMapper.selectByPrimaryKey(newid);
        if (articleCate == null){
            return false;
        }
        articleCate.setIsDelete(true);
        int flag = articleCateMapper.updateByPrimaryKeySelective(articleCate);
        if (flag != 1){
            throw new SystemException("delete failed", ExType.SystemInsertFail);
        }
        return true;
    }

    /**
     * 根据id 获取文章类别信息：
     * 1、根据id查询数据库文章类别 没找到抛异常
     * 2、找到了将 articleCate 返回给控制层
     * @param id
     * @return
     */
    @Override
    public ArticleCate getCateById(String id) {

        ArticleCate articleCate = articleCateMapper.selectByPrimaryKey(Integer.parseInt(id));
        if(articleCate == null){
            throw new BusinessException("articleCate not found by "+id,ExType.BussRecordNotFound);
        }
        return articleCate;
    }

    /**
     * 更新类别信息：
     * 1、根据类别名在数据库查询有无相同类别，有记录返回false
     * 2、无记录进行修改操作，返回true
     * @param articleCate
     * @return
     */
    @Override
    public Boolean updateCate(ArticleCate articleCate) {
        ArticleCateExample articleCateExample = new ArticleCateExample();
        articleCateExample.createCriteria().andNameEqualTo(articleCate.getName());
        List<ArticleCate> cateList = articleCateMapper.selectByExample(articleCateExample);
        if (cateList != null && !cateList.isEmpty()){
            return false;
        }

        //2、无记录进行修改操作，返回true
        int flag = articleCateMapper.updateByPrimaryKeySelective(articleCate);
        if (flag != 1){
            throw new SystemException("update failed", ExType.SystemInsertFail);
        }
        return true;
    }
}
